package com.headcaselabs.lb.client;

import java.io.IOException;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.headcaselabs.lb.common.Director;

/* 
Copyright 2007, HeadCase Humanufacturing, Inc.

	Licensed under the Apache License, Version 2.0 (the "License"); you
	may not use this file except in compliance with the License. You may
	obtain a copy of the License at
	
	   http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
	implied. See the License for the specific language governing
	permissions and limitations under the License.
*/
/**
 * Connects to a director and dumps node information and client-node mapping information.
 * @author Florian Leibert
 * 
 */
public class NodeInformation {
	
	protected Director director;
	
	/**
	 * Default Constructor
	 * @throws RemoteException
	 */
	public NodeInformation(int port) throws RemoteException {
			try {
				Registry registry = LocateRegistry.getRegistry(port);
				director = (Director) registry.lookup("Director");
			} catch (NotBoundException nbe) {

				nbe.printStackTrace();
				System.exit(1);
			}
	}
	
	/**
	 * Obtains and prints information
	 * @param type of information
	 */
	public void printInfo(String type) throws RemoteException {
		if (type.equals("all")) {
			System.out.println("Dumping information node information: ");
			System.out.println(director.getServerDump());
			System.out.println();
			System.out.println("Dumping information from mapping table: ");
			System.out.println(director.getMappingDump());
			System.out.println();
			System.out.println("Dumping information from the database: ");
			System.out.println(director.getDBDump());
		}
		
	}

	@SuppressWarnings("static-access")
	public static void main(String[] args) throws IOException {
		int port = Director.PORT;
		HelpFormatter formatter = new HelpFormatter();
		String type = null;
		//TODO: allow specifying more launch arguments
		if (args.length>0) {
			Options options = new Options();
			options.addOption(OptionBuilder.withArgName("port").hasArg().withDescription("port of the RMI registry").create("p"));
			options.addOption(OptionBuilder.withArgName("type").hasArg().withDescription("type of information to dump - currently only all is supported").isRequired(true).create("t"));
			
			options.addOption("help",true,"this message");
			try {
				CommandLineParser parser = new GnuParser();
				CommandLine cmd = parser.parse( options, args);
				if (cmd.hasOption("p")) {
					port = Integer.parseInt(cmd.getOptionValue("p"));
				} 
				if (cmd.hasOption("help")) {
					formatter.printHelp("PowerPipe",options);
					System.exit(0);
				}
				type = cmd.getOptionValue("t");
			
			} catch (ParseException pe) {
				 System.err.println( "Invalid parameters.  Reason: " + pe.getMessage() );
				 formatter.printHelp("NodeInformation",options);
				 System.exit(1);
			}
		}
		NodeInformation info = new NodeInformation(port);
		info.printInfo(type);
		
	}
}
